package com.example.phone.home;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.phone.ProductDetails.Detail;
import com.example.phone.R;
import com.example.phone.bean.Category;
import com.example.phone.bean.Product;
import com.loopj.android.image.SmartImageView;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class SortActivity extends AppCompatActivity implements View.OnClickListener {

    private ProgressBar sort_progressBar;
    private ScrollView sort_ScrollView;
    private List<Category> categoryList = new ArrayList<>();
    private List<Product> productDetailList = new ArrayList<>();
    public static final String path = "http://120.77.253.51:8080/PhoneShop/Images/";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_sort );
        initData();
    }

    //初始化数据
    private void initData() {
        //获取携带的商品分类ID
        Intent intent = getIntent();
        int categoryId = intent.getIntExtra("categoryId", 1);
        //Log.d("==========category", categoryId + "");

        //匿名回调接口获取分类商品信息
        RequestData requestData = new RequestData(new RequestData.UpdateData() {
            @Override
            public void onSuccess(Object object) {
                categoryList = (List<Category>) object;
                getProductDetailList(categoryList);
            }

            @Override
            public void onFail(String error) {
                Toast.makeText(getApplicationContext(), error, Toast.LENGTH_SHORT).show();
            }
        });
        requestData.sendRequestCategoryProduct(categoryId);
    }

    //根据商品ID查询商品详情并封装到集合中
    private List<Product> getProductDetailList(final List<Category> categoryList) {
        for (int i = 0; i < categoryList.size(); i++) {
            RequestData requestData1 = new RequestData(new RequestData.UpdateData() {
                @Override
                public void onSuccess(Object object) {
                    Product product = (Product) object;
                    productDetailList.add(product);
                    if (productDetailList.size() == categoryList.size()) {
                        details(productDetailList);
                    }
                }

                @Override
                public void onFail(String error) {
                    Toast.makeText(getApplicationContext(), error, Toast.LENGTH_SHORT).show();
                }
            });
            requestData1.sendRequestProductDetails(categoryList.get(i).getId() - 7);
        }
        return productDetailList;
    }

    //商品展示
    private void details(final List<Product> productDetailList) {

        //设置商品推荐位
        setRecommend(productDetailList.get(0));

        GridView gridview = (GridView)findViewById( R.id.sort_details );
        gridview.setFocusable( false );
        GridViewAdapter adapter = new GridViewAdapter( this, productDetailList );
        gridview.setAdapter( adapter );

        //添加点击事件
        gridview.setOnItemClickListener( new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                Intent intent = new Intent( getApplicationContext(), Detail.class );
                Bundle bundle = new Bundle();
                bundle.putInt("productId", productDetailList.get(arg2).getId());
                intent.putExtras(bundle);
                startActivity( intent );
            }}
        );
        sort_progressBar = findViewById(R.id.sort_progressBar);
        sort_ScrollView = findViewById(R.id.sort_scrollView);
        sort_progressBar.setVisibility(View.GONE);
        sort_ScrollView.setVisibility(View.VISIBLE);
    }

    //设置推荐商品信息
    private void setRecommend(Product product) {
        LinearLayout sort_recommend = findViewById(R.id.sort_recommend);
        SmartImageView sort_new_img = findViewById( R.id.sort_new_img );
        TextView sort_new_name = findViewById( R.id.sort_new_name );
        TextView sort_new_price = findViewById( R.id.sort_new_price);

        sort_new_img.setImageUrl( path + product.getMainImage() );
        sort_new_name.setText( product.getName() );
        sort_new_price.setText( "¥" + product.getPrice().setScale(1, BigDecimal.ROUND_HALF_UP));

        sort_recommend.setOnClickListener(this);
    }

    //推荐最新商品点击事件
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.sort_recommend:
                Intent intent = new Intent( getApplicationContext(), Detail.class );
                Bundle bundle = new Bundle();
                bundle.putInt("productId", productDetailList.get(0).getId());
                intent.putExtras(bundle);
                startActivity( intent );
                break;
        }
    }


}
